package com.harmontronics.hanma.paas.paas.domain.organization.entity;

import com.harmontronics.hanma.paas.paas.domain.organization.entity.OrgUserExcel;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

@Data
public class OrgUserExport extends OrgUserExcel {

    private List<OrgTree> orgTreeList;

    private Long higherId;

    @Override
    public String getOrgName() {
        String result = "";

        for(OrgTree tree : orgTreeList){
            String tmpResult = "";
            if(CollectionUtils.isEmpty(tree.getChildren())){
                tmpResult = "/" + tree.getName() +";" ;
            }else{
                tmpResult = addOrgName(tmpResult,tree.getChildren()) +"/"+ tree.getName() + ";";
            }
            result = tmpResult + result;
        }

        if (result.endsWith(";")) {
            result = result.substring(0,result.length()-1);
        }
        String[] results = result.split(";");
        List<String> resultList = Arrays.stream(results).map(tmp -> tmp.substring(1)).collect(Collectors.toList());
        result = resultList.stream().collect(Collectors.joining(";"));
        return result ;
    }

    private String addOrgName(String result, List<OrgTree> children) {
        if (CollectionUtils.isNotEmpty(children)) {
            for (OrgTree treeDto : children) {
                result = "/" + treeDto.getName() + result;
                result = addOrgName(result, treeDto.getChildren());
            }
        }
        return result;
    }

}
